# Build recipe for sinit.
#
# Copyright (c) 2016-2018 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

program=sinit
version=1.0
release=1

tarname=${program}-${version}.tar.bz2

# Remote source(s)
fetch=http://git.suckless.org/sinit/snapshot/$tarname

description="
Suckless init.

Sinit is a simple init.  It was initially based on
Rich Felker's minimal init.

There are 3 signals that sinit(8) will act on.

  SIGUSR1: powers off the machine.
  SIGINT:  reboots the machine (or alternatively via ctrl-alt-del).
  SIGCHLD: reap children
"

homepage=http://core.suckless.org/sinit
license=MIT/X

# Source documentation
docs="LICENSE README"
docsdir="${docdir}/${program}-${version}"

build()
{
    set -e

    unpack "${tardir}/$tarname"

    cd "$srcdir"

    patch -p0 < "${worktree}/patches/sinit/dragora-Makefile.diff"
    patch -p0 < "${worktree}/patches/sinit/dragora-config.def.h.diff"

    make -j${jobs} CFLAGS="$QICFLAGS -Wextra -Wall" \
                   LDFLAGS="$QILDFLAGS -static" \
                   PREFIX=/usr \
                   DESTDIR="$destdir" install

    mkdir -p "${destdir}/usr/sbin"

    # Set sinit as the default system init
    ( cd "${destdir}/usr/sbin" && mv sinit init )

    # Provide halt and killall5 implementations from "ubase",
    # modifications of Dimitris Papastamos (a.k.a sin)

    mkdir -p halt killall5
    cp -p "${worktree}"/archive/sinit/halt/* halt/
    cp -p "${worktree}"/archive/sinit/killall5/* killall5/

    cd halt
    cc $QICFLAGS -Wextra -Wall -static -o halt *.c
    strip --strip-unneeded halt

    cp -p halt "${destdir}/usr/sbin/"
    chmod 755 "${destdir}/usr/sbin/halt"
    cp -p halt.8 "${destdir}/${mandir}/man8/"

    cd ../killall5
    cc $QICFLAGS -Wextra -Wall -static -o killall5 *.c
    strip --strip-unneeded killall5

    cp -p killall5 "${destdir}/usr/sbin/"
    chmod 755 "${destdir}/usr/sbin/killall5"
    cp -p killall5.8 "${destdir}/${mandir}/man8/"

    cd ..

    for script in reboot shutdown
    do
        cp -p "${worktree}/archive/sinit/${script}" "${destdir}/usr/sbin/"
        chmod 751 "${destdir}/usr/sbin/${script}"
    done
    unset script

    ln -s shutdown "${destdir}/usr/sbin/poweroff"

    # Compress man page(s)
    lzip -9 "${destdir}/${mandir}"/man8/*.8

    # Copy documentation
    mkdir -p "${destdir}${docsdir}"
    cp -p $docs "${destdir}${docsdir}/"
}

